package com.cjfan.trendplus.noteit.wordbank;

import com.cjfan.trendplus.noteit.NoteIT;
import com.cjfan.trendplus.noteit.R;
import com.cjfan.trendplus.noteit.add.AddNewItemActivity;
import com.cjfan.trendplus.noteit.add.RecordActivity;
import com.cjfan.trendplus.sqlit.DatabaseHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class WordbankActivity extends Activity {
	private DatabaseHandler db=null;
	TextView chinese;
	Button btnDelete, homeBtn;
	ListView listview01;
	Cursor cursor;
	long myid;  // 儲存 _id 的值
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wordbank);
        chinese = (TextView)findViewById(R.id.chinese);
        listview01=(ListView)findViewById(R.id.ListView01);
        btnDelete=(Button)findViewById(R.id.btnDelete);
        homeBtn = (Button)findViewById(R.id.homeBtn);
        
        //listener
        homeBtn.setOnClickListener(btnTrigger);
        listview01.setOnItemClickListener(listview01Listener); 
        
        db =new DatabaseHandler(this);       // 建立 MyDB 物件
        db.open();  
        cursor=db.getAll();// 載入全部資料
        UpdataAdapter(cursor);  // 載入資料表至 ListView 中
    }
	
	private ListView.OnItemClickListener listview01Listener = new ListView.OnItemClickListener(){
		public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
			ShowData(id);
			cursor.moveToPosition(position);
			
			AlertDialog.Builder builder = new AlertDialog.Builder(WordbankActivity.this);
			{
				builder.setTitle("選擇你所要的動作");
		  		builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
		  		// do something when the button is clicked
			    public void onClick(DialogInterface arg0, int arg1) {
				  // to be continue...
			    }
		  		});
		  		builder.setNegativeButton("刪除", new DialogInterface.OnClickListener() {
			  	     // do something when the button is clicked
	  		  		 public void onClick(DialogInterface arg0, int arg1) {
	  		  			if (cursor != null && cursor.getCount() >= 0){
		    	        	AlertDialog.Builder builderIn=new AlertDialog.Builder(WordbankActivity.this);
		    	        	builderIn.setTitle("確定刪除嗎？");
		    	        	builderIn.setMessage("確定要刪除" + chinese.getText() + "這筆資料?"); 		    	        	
		    	        	builderIn.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		    	            	public void onClick(DialogInterface dialog, int i) {
		    	    			}
		    	    		});
		    	        	builderIn.setPositiveButton("確定",new DialogInterface.OnClickListener() {
		    	        		public void onClick(DialogInterface dialog, int i) {
		    	        			if (db.delete(myid)){
			    	        	        cursor=db.getAll();// 載入全部資料
			    	        	        UpdataAdapter(cursor); // 載入資料表至 ListView 中
			    	        	        ClearEdit();
		    	        			}
		    					}
		    				});
		    	        	builderIn.show();	
		    			}
			  	     }
		  		});
		  		builder.show();
			}
		}
	};
	
	private OnClickListener btnTrigger=new OnClickListener(){
		@Override
		public void onClick(View v) {
			switch(v.getId()){
				case R.id.homeBtn:
				{
					Intent intentHome = new Intent();
					intentHome.setClass(WordbankActivity.this, NoteIT.class);
			    	startActivity(intentHome);
			    	WordbankActivity.this.finish();
				  	break;
				}
				
				default:
			}
		}
    };
    
	private void ShowData(long id){ //顯示單筆資料
    	Cursor c=db.get(id);
    	myid=id;  // 取得  _id 欄位 
    	
    	chinese.setText(c.getString(1)); // name 欄位
    	
    }  
	
    
    public void ClearEdit(){
		chinese.setText("");	
	}
	
	public void UpdataAdapter(Cursor cursor){
		if (cursor != null && cursor.getCount() >= 0){
			SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,							
			android.R.layout.simple_list_item_2, // 包含兩個資料項			
			cursor, // 資料庫的 Cursors 物件			
			new String[] { "chinese", "english" }, // name、price 欄位
			new int[] { android.R.id.text1, android.R.id.text2 }); 
			listview01.setAdapter(adapter); // 將adapter增加到listview01中 	
		}
	} 
}
